package com.wuyong.chapter4;

/**
 * 链表底层实现 修改为虚拟结点以后
 *
 * @param <E>
 */
public class LinkedList<E> {

    private class Node {
        private E e;
        private Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this.e = e;
            this.next = null;
        }

        public Node() {
            this.e = null;
            this.next = null;
        }

        @Override
        public String toString() {
            if (e != null) {
                return e.toString();
            }
            return "";
        }
    }

    private int size;
    private Node dummyHead;

    public LinkedList() {
        dummyHead = new Node();
        size = 0;
    }

    //获取链表大小
    public int getSize() {
        return size;
    }

    // 修改链表的第index(0-based)个位置的元素为e
    // 在链表中不是一个常用的操作，练习用：） O(n)
    public void set(int index, E e){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Illegal index.");

        Node cur = dummyHead.next;
        for(int i = 0 ; i < index ; i ++)
            cur = cur.next;
        cur.e = e;
    }

    //判断链表是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //在链表中插入数据
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is illegal");
        }

        Node pre = dummyHead;
        //找前一个元素
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        pre.next = new Node(e, pre.next);
        size++;
    }

    //在链表头部添加数据 0(1)
    public void addFirst(E e) {
        add(0, e);
    }

    //在链表末尾添加数据 O(n)
    public void addLast(E e) {
        add(size, e);
    }

    //根据索引获取元素 平均时间复杂度 O(n/2) = O(n)
    public E get(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is illegal");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
       return cur.e;
    }
    //获取第一个元素
    public E getFirst() {
        return get(0);
    }
    //获取最后一个元素
    public E getLast() {
        return get(0);
    }
    //链表中是否包含某元素 O(n)
    public boolean contain(E e) {
        Node cur = dummyHead.next;
        while (cur != null) {
            if (cur.e.equals(e)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 查找链表中是否有元素e
//    public boolean contains(E e){
//        Node cur = dummyHead.next;
//        while(cur != null){
//            if(cur.e.equals(e))
//                return true;
//            cur = cur.next;
//        }
//        return false;
//    }

    //从列表中删除元素
    public E remove(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is illegal");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;
        size --;
        return delNode.e;
    }
    //删除列表第一个元素 0(1)
    public E removeFirst() {
        return remove(0);
    }
    //删除列表最后一个元素 O(n)
    public E removeLast() {
        return remove(size);
    }

    // 从链表中删除元素e
    public void removeElement(E e){

        Node prev = dummyHead;
        while(prev.next != null){
            if(prev.next.e.equals(e))
                break;
            prev = prev.next;
        }

        if(prev.next != null){
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            size --;
        }
    }

    /**
     * 递归实现removeElement
     * @param e
     */
    public void removeElement2(E e){
        removeElement(dummyHead, e);
    }

    private Node removeElement(Node node, E e) {
        if (node.next.e.equals(e)) {
            Node delNode = node.next;
            node.next = delNode.next;
            delNode.next = null;
            size --;
            return node;
        }
        node.next = removeElement(node.next, e);
        return node;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("LinkedList size = %d .", size));
        Node cur = dummyHead.next;
        while (cur != null) {
            sb.append(cur.e + "->");

            cur = cur.next;
        }
        sb.append("NULL");
        return sb.toString();
    }
}
